package com.yyjj.zixun.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.aries.ui.view.title.TitleBarView;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.zixun.R;
import com.yyjj.zixun.api.Api;
import com.yyjj.zixun.entity.NeteaseEntity;
import com.zzhoujay.richtext.RichText;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.constant.ApiConstant;

public class QuestionnaireActivity extends BaseTitleActivity {

    String url = "";
    String type = "";
    TextView title;
    TextView content;


    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("详情").setTextColor(Color.WHITE).setBackground(getResources().getDrawable(R.drawable.btn_jb));
    }

    @Override
    public int getContentLayout() {
        return R.layout.zixun_activity_netease_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        title = findViewById(R.id.tv_title);
        content = findViewById(R.id.tv_content);

        url = (String) getIntent().getSerializableExtra("url");
        type = (String) getIntent().getSerializableExtra("type");

        if (type != null){
            getQKLDatas(url);
        }else {
            getDatas(url);
        }



    }
    private void getQKLDatas(String url) {
        showLoading();
        ViseHttp.GET(Api.QUKUAILIAN_DETAILS)
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("name", "details")
                .addParam("url", url)
//                .setHttpCache(true)
//                .cacheMode(CacheMode.FIRST_REMOTE)
                .request(new ACallback<NeteaseEntity>() {
                    @Override
                    public void onSuccess(NeteaseEntity data) {
                        try {
                            if (data.getData() != null) {
                                title.setText(data.getData().getTitle());
                                RichText.initCacheDir(mContext);
                                RichText.fromHtml(data.getData().getContent()).clickable(false).into(content);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hideLoading();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                    }
                });
    }


    private void getDatas(String url) {
        showLoading();
        ViseHttp.GET(Api.CONTENT_URL)
                .baseUrl(ApiConstant.BASE_URL)
                .addParam("content_url", url)
//                .setHttpCache(true)
//                .cacheMode(CacheMode.FIRST_REMOTE)
                .request(new ACallback<NeteaseEntity>() {
                    @Override
                    public void onSuccess(NeteaseEntity data) {
                        try {
                            if (data.getData() != null) {
                                title.setText(data.getData().getTitle());
                                RichText.initCacheDir(mContext);
                                RichText.fromHtml(data.getData().getContent()).clickable(false).into(content);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hideLoading();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                    }
                });
    }
}
